package com.jianganming.observer.demo;

/**
 * 观察者模式
 */
public class ObserverPattern {

    public static void main(String[] args) {

        Subject concreteSubject = new Subject("王老师") {
            @Override
            public void notifyObserver(Subject subject) {
                //System.out.println(subject.getName()+"：同学们开始上课了");
                for (Observer observer : this.observers) {
                    observer.response(subject);
                }
            }
        };


        concreteSubject.add((subject)-> {
            System.out.println(subject.getName()+": 通知");
            System.out.println("张三：好的，我这就去上课");

        });
        concreteSubject.add((subject)-> {
            System.out.println(subject.getName()+": 通知");
            System.out.println("李四：老师，稍等会，我的作业还没做完");
        });
        concreteSubject.add((subject)-> {
            System.out.println(subject.getName()+": 通知");
            System.out.println("王武：我还想睡觉");
        });
        concreteSubject.notifyObserver(concreteSubject);
    }
}
